﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DYSmartNetClient;
using System.Text;

namespace TestSLClient
{
    public partial class MainPage : UserControl
    {
        DYNet con;

        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
            button1.Click += new RoutedEventHandler(button1_Click);
        }

        void button1_Click(object sender, RoutedEventArgs e)
        {
            con.Send(Encoding.UTF8.GetBytes(textBox1.Text));
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            con = new DYNet();
            con.OnConnectEvent += new OnConnectDelegate(con_OnConnectEvent);
            con.OnDataEvent += new OnDataDelegate(con_OnDataEvent);
            con.Connect(ServerIP: "localhost", port: 4504);

            con.ConnectToServer(Encoding.UTF8.GetBytes("内Cool超人"));
        }

        void con_OnConnectEvent(byte[] data)
        {
            if (data != null)
            {
                listBox1.Items.Insert(0, Encoding.UTF8.GetString(data, 0, data.Length) + " Connect ok!!!");
            }
        }

        void con_OnDataEvent(byte[] data)
        {
            listBox1.Items.Insert(0, Encoding.UTF8.GetString(data, 0, data.Length));
        }
    }
}
